﻿using System;
using System.Collections.Generic;
using BoldBrick.OneIntranet.HR.DataObjects;
using BoldBrick.OneIntranet.HR.DataObjects.ContentTypes;
using BoldBrick.OneIntranet.HR.FieldTypes;
using Microsoft.SharePoint;
using BoldBrick.SharePoint.CommonControls.Extensions;
using System.Linq;

namespace BoldBrick.OneIntranet.HR.Workflows
{
	[Serializable]
	public class HRWorkflowStepData
	{
		/// <summary>
		/// User ID of task owner
		/// </summary>
		public int? TaskOwnerID { get; set; }
		
		/// <summary>
		/// Content type for result item
		/// </summary>
		public ContentTypeLookupValue ResultContentType { get; set; }
		
		/// <summary>
		/// Content type fot task item
		/// </summary>
		public ContentTypeLookupValue TaskContentType { get; set; }

		/// <summary>
		/// Work flow step content type
		/// </summary>
		public ContentTypeLookupValue WorkflowStepContentType { get; set; }

		/// <summary>
		/// Title of the task
		/// </summary>
		public string TaskTitle { get; set; }

		/// <summary>
		/// Description of the task
		/// </summary>
		public string TaskDescription { get; set; }

		/// <summary>
		/// Order of this step
		/// </summary>
		public double? Order { get; set; }

		/// <summary>
		/// If true workflow is canceled when task is finished with "No hire" result
		/// </summary>
		public bool CancelIfNoHire { get; set; }

		/// <summary>
		/// Extended properties which are in inherited content type from Workflow Step content type
		/// </summary>
		public Dictionary<string, string> ExtendedProperties = new Dictionary<string, string>();

		/// <summary>
		/// Gets true if email has to be sent
		/// </summary>
		public bool SendEmail { get; set; }

		/// <summary>
		/// ID of workflow step
		/// </summary>
		public int ID { get; set; }

		public HRWorkflowStepData()
		{
			
		}
		
		public HRWorkflowStepData(SPWeb web, SPListItem itemWorkflowStep)
		{
			SPFieldUserValue taskOwner = itemWorkflowStep.GetValueObject<SPFieldUserValue>(SiteColumns.TaskOwnerColumn.InternalName);
			if (taskOwner != null)
			{
				this.TaskOwnerID = taskOwner.LookupId;
			}

			ID = itemWorkflowStep.ID;

			ResultContentType = itemWorkflowStep.GetValueObject<ContentTypeLookupValue>(SiteColumns.ResultContentTypeLookupColumn.InternalName);
			TaskContentType = itemWorkflowStep.GetValueObject<ContentTypeLookupValue>(SiteColumns.TaskContentTypeLookupColumn.InternalName);
			WorkflowStepContentType = new ContentTypeLookupValue(itemWorkflowStep.ContentTypeId.ToString(), itemWorkflowStep.ContentType.Name);

			TaskTitle = itemWorkflowStep.GetValueObject<string>(SiteColumns.TaskTitleColumn.InternalName);
			TaskDescription = itemWorkflowStep.GetValueObject<string>(SiteColumns.TaskDescriptionColumn.InternalName);
			Order = itemWorkflowStep.GetValue<double>(SiteColumns.WorkflowStepOrderColumn.InternalName);
			
			bool? cancelIfNoHire = itemWorkflowStep.GetValue<bool>(SiteColumns.CancelIfNoHireColumn.InternalName);
			CancelIfNoHire = cancelIfNoHire.HasValue && cancelIfNoHire.Value;
			
			bool? sendEmail = itemWorkflowStep.GetValue<bool>(SiteColumns.SendEmailColumn.InternalName);
			SendEmail = sendEmail.HasValue && sendEmail.Value;

			// load extended properties = all fields that are not in parent content type (WorkflowStepContentType)
			SPContentType workflowStepContentType = web.ContentTypes.GetChildContentType(ContentTypes.WorkflowStepContentType.ID);
			string[] workflowStepFields = workflowStepContentType.FieldLinks.OfType<SPFieldLink>().Select(item => item.Name).ToArray();
			
			SPContentType itemContentType = itemWorkflowStep.ContentType;
			string[] itemFields = itemContentType.FieldLinks.OfType<SPFieldLink>().Select(item => item.Name).ToArray();
			IEnumerable<string> fields = itemFields.Except(workflowStepFields);

			foreach (string field in fields)
			{
				object value = itemWorkflowStep[field];
				if (value != null)
				{
					this.ExtendedProperties[field] = value.ToString();
				}
			}
		}
	}
}
